package cn.t.web.device;

import cn.t.core.mode.PageInfoDTO;
import cn.t.core.mode.PageQueryDTO;
import cn.t.dto.device.DeviceDTO;
import cn.t.dto.order.ReplenishmentRecordDTO;
import cn.t.facade.device.DeviceFacade;
import cn.t.facade.order.ReplenishmentRecordFacade;
import cn.t.request.CreateDeviceReq;
import cn.t.request.DeviceConfigReq;
import cn.t.request.OnOffShelfReq;
import cn.t.request.QueryDeviceReq;
import cn.t.request.UploadDeviceLogReq;
import cn.t.vo.HardwareConfigDetailVO;
import cn.t.vo.HardwareConfigVO;
import cn.t.vo.device.DeviceBaseInfoVO;
import cn.t.vo.device.DeviceConfigVO;
import cn.t.vo.device.DeviceDetailVO;
import cn.t.vo.device.DeviceOverviewVO;
import cn.t.vo.device.DeviceVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 设备管理-设备列表
 *
 * @author t
 * @since 2024-05-24 10:15:13
 */
@Slf4j
@RestController
@RequestMapping("/device")

@AllArgsConstructor
public class DeviceController {

    private final DeviceFacade deviceFacade;
    private final ReplenishmentRecordFacade replenishmentRecordFacade;

    /**
     * 设备列表
     */
    @PostMapping("/page")
    public PageInfoDTO<DeviceVO> page(@RequestBody PageQueryDTO<QueryDeviceReq> request) {
        return deviceFacade.page(request);
    }

    /**
     * 设备概览
     */
    @GetMapping("/overview/{firmId}")
    public DeviceOverviewVO overview(@PathVariable Long firmId) {
        return deviceFacade.overview(firmId);
    }

    /**
     * 新增设备
     */
    @PostMapping("/add")
    public Boolean add(@Validated @RequestBody CreateDeviceReq request) {
        return deviceFacade.add(request);
    }

    /**
     * 设备配置列表
     */
    @PostMapping("/config/page")
    public PageInfoDTO<DeviceConfigVO> configPage(@RequestBody PageQueryDTO<DeviceConfigReq> request) {
        return this.deviceFacade.configPage(request);
    }

    /**
     * 硬件配置列表
     */
    @PostMapping("/hardware/page")
    public PageInfoDTO<HardwareConfigVO> hardwareConfigPage(@RequestBody PageQueryDTO<DeviceConfigReq> request) {
        return this.deviceFacade.hardwarePage(request);
    }

    /**
     * 编辑设备
     */
    @PostMapping("/config/update")
    public Boolean update(@RequestBody DeviceDTO request) {
        return this.deviceFacade.updateDevice(request);
    }

    /**
     * 设备详情
     */
    @GetMapping("/detail/{deviceId}")
    public DeviceDetailVO detail(@PathVariable Long deviceId) {
        return this.deviceFacade.detail(deviceId);
    }

    /**
     * 补货记录 - 库存调整记录
     */
    @PostMapping("/inventoryAdjustRecords")
    public PageInfoDTO<ReplenishmentRecordDTO> findByPage(@RequestBody PageQueryDTO<ReplenishmentRecordDTO> queryDTO) {
        List<ReplenishmentRecordDTO> list = this.replenishmentRecordFacade.queryList(queryDTO.getParam(), queryDTO.getStart(), queryDTO.getPageSize(), " gmt_created", " desc");
        if (CollectionUtils.isEmpty(list)) {
            return new PageInfoDTO<>(queryDTO.getPageNo(), queryDTO.getPageSize(), 0L, null);
        } else {
            long total =  this.replenishmentRecordFacade.count(queryDTO.getParam());

            return new PageInfoDTO<>(queryDTO.getPageNo(), queryDTO.getPageSize(), total, list);
        }
    }

    /**
     * 上下架、设置
     */
    @PostMapping("/onOffShelf")
    public Boolean onOffShelf(@Validated @RequestBody OnOffShelfReq request) {
        return this.deviceFacade.onOffShelf(request);
    }

    /**
     * 硬件配置详情
     */
    @GetMapping("/hardware/{deviceId}")
    public HardwareConfigDetailVO hardwareConfigDetail(@PathVariable Long deviceId) {
        return this.deviceFacade.hardwareConfigDetail(deviceId);
    }

    /**
     * 设备基础信息
     */
    @GetMapping("/baseInfo/{deviceId}")
    public DeviceBaseInfoVO deviceBaseInfo(@PathVariable Long deviceId) {
        return this.deviceFacade.deviceBaseInfo(deviceId);
    }









}

